//
//  BaoXiaoDetailViewController.swift
//  HuaYun
//
//  Created by Mac on 2019/3/20.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class BaoXiaoDetailViewController: PagedViewController {

    var id  = ""
    let approvalBtn = UIButton.init(type: .system)
    var canapproval = false
    @objc func approvalAction(button: UIButton){
        let approval = ApprovalSuggestionViewController()
        
        approval.applyId = self.id
        
        approval.netPath = ApprovalBXAction
        
        approval.businessType = "140"
        
        self.navigationController?.present(vc: approval)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.load(with: "")
        
        self.view.addSubview(approvalBtn)
        
        approvalBtn.setTitle("审批意见", for: .normal)
        
        approvalBtn.setTitleColor(UIColor.white, for: .normal)
        
        approvalBtn.setTitleColor(UIColor.hex("999999"), for: .disabled)
        
        approvalBtn.backgroundColor = UIColor.mainColor
        
        approvalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        approvalBtn.addTarget(self, action: #selector(approvalAction(button:)), for: .touchUpInside)
        
        self.approvalBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
//            make.height
            if self.canapproval {
                make.height.equalTo(55)
            } else {
                make.height.equalTo(0)
            }
        }
        
        self.page.view.snp.makeConstraints { (make) in
            make.bottom.equalTo(approvalBtn.snp.top)
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(55)
            make.width.equalToSuperview()
        }
        
        
        for view in self.view.subviews {
            if view is UIScrollView {
                let scroll = view as! UIScrollView
                scroll.bounces = false
            }
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(popobjc), name: NSNotification.Name("RefreshDetail"), object: nil)
        // Do any additional setup after loading the view.
    }
    
    
    
    
    
    override func path(for name: String) -> String {
        return ALLBXDetail
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["id": self.id]
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            self.endLoading()
            var contents: [Dictionary<String,String?>] = []
            contents.append(["报销申请单号:": json["data"]["details"]["serialNumber"].string])
            let having = json["data"]["details"]["yesOrNotContract"].int ?? -1
            if having == 0 {
                contents.append(["有无合同:": "手工创建合同"])
            } else {
                contents.append(["有无合同:": "无合同"])
            }
            contents.append(["申请人:": json["data"]["details"]["applyUser"].string])
            contents.append(["报销人:": json["data"]["details"]["concernedHuman"].string])
            let type = (json["data"]["details"]["reimbursementBigTypeName"].string ?? "") + "," + (json["data"]["details"]["reimbursementSmallTypeName"].string ?? "")
            contents.append(["报销类型:": type])
            contents.append(["联系电话:": json["data"]["details"]["phone"].string])
            let projects = json["data"]["details"]["reimbursementProjects"].array ?? []
            let total = (json["data"]["details"]["reimbursementMoney"].double ?? 0.0) / 100.0
            contents.append(["报销含税总金额(元):": String.init(format: "%.2f", total)])
            contents.append(["报销含税总金额大写:": String.init(format: "%.2f", total).rmbOldStyle])
            let nottotal = (json["data"]["details"]["reimbursementMoneyNotTax"].double ?? 0.0) / 100.0
            contents.append(["报销不含税总金额(元):": String.init(format: "%.2f", nottotal)])
            let remaining = total - nottotal
            contents.append(["报销不含税总金额(元):": String.init(format: "%.2f", remaining)])
            contents.append(["申请日期:": json["data"]["details"]["applyTime"].string])
            let money = json["data"]["details"]["reimbursementPayMoney"].double ?? 0.0
            contents.append(["报销付款金额:": String(format: "%.2f", money / 100.0)])
            let first = self.ctls[0] as! BXDtlViewController
            first.projects = projects
            first.headsource = contents
            
            let details = json["data"]["details"]["reimbursementDetails"].array ?? []
            first.tickets = details
            var bottomcontents: [Dictionary<String,String?>] = []
//            let jhhs = json["data"]["details"]["auditMoney"].double ?? 0
//            let jhhsyuan = String(format: "%.2f", jhhs / 100.0)
//            bottomcontents.append(["稽核含税金额(元)": jhhsyuan])
//            let jhbhs = json["data"]["details"]["auditMoneyNotTax"].double ?? 0.0
//            let jhbhsyuan = String(format: "%.2f", jhbhs / 100.0)
//            bottomcontents.append(["稽核不含税金额(元)": jhbhsyuan])
//            let remain = jhhs - jhbhs
//            let remainyuan = String(format: "%.2f", remain / 100.0)
//            bottomcontents.append(["稽核税额(元):": remainyuan])
//            bottomcontents.append(["稽核日期:": json["data"]["details"]["auditTime"].string])
            let real = json["data"]["details"]["actuallyPayMoney"].double ?? 0
            let realyuan = String(format: "%.2f", real / 100)
            bottomcontents.append(["实付金额(元):": realyuan])
            bottomcontents.append(["实付日期:": json["data"]["details"]["actuallyPayTime"].string])
            bottomcontents.append(["报销事由:": json["data"]["details"]["reimbursementReason"].string])
            let document = json["data"]["details"]["attachFiles"].array ?? []
            first.bottomsource = bottomcontents
            first.documents = document
            first.tableView.reloadData()
            let second = self.ctls[1] as! ApprovalRecordListViewController
            second.source = json["data"]["approvalList"].array ?? []
            second.tableView.reloadData()
        }
        return suc
    }
    
    
    override func configTitles() -> Array<String> {
        return ["基本信息","审批记录"]
    }
    
    override func configViewControllers() -> [UIViewController] {
        let purchase = BXDtlViewController()
        return [purchase,ApprovalRecordListViewController()]
    }

}
